package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.DepartmentDTO;
import com.lastproject.ejb.entities.Department;

@Stateless
@LocalBean
public class DepartmentConversion {
	
	@PersistenceContext
	EntityManager em;

	public DepartmentDTO fromEntity(Department department){
		
		DepartmentDTO departmentDTO = new DepartmentDTO();
		departmentDTO.setId(department.getId());
		departmentDTO.setName(department.getName());
		departmentDTO.setDescription(department.getDescription());
		
		return departmentDTO;

	}
	
	public Department fromDTO(DepartmentDTO departmentDTO){

			Department department = em.find(Department.class, departmentDTO.getId());

			if (department == null) {
				department = new Department();
			}

			department.setName(departmentDTO.getName());
			department.setDescription(departmentDTO.getDescription());
			
			return department;
		
	}
	
	
}
